package com.sunyard.pcc.base;



import com.sunyard.pcc.ClassReflectUtil;
import com.sunyard.pcc.RandomUtils;

import java.util.List;

public abstract class BaseService<T> {

    public abstract BaseDao<T> getDAO();

    public void save(T t)  throws Exception{
        //设置主键.字符类型采用UUID,数字类型采用自增


        Object obj= ClassReflectUtil.getFieldValue(t,"id");

        if((obj==null||"".equals(obj))){
            ClassReflectUtil.setIdKeyValue(t,"id", RandomUtils.getUUID());
        }
        getDAO().save(t);
    }

    public void update(T t) {
        getDAO().update(t);
    }


    public void updateBySelective(T t){
        getDAO().updateBySelective(t);
    }

    public void delete(Object... ids) {
        if(ids == null || ids.length < 1){
            return;
        }
        for(Object id : ids ){
            getDAO().delete(id);
        }
    }

    public int count(BaseVo vo) {
        return getDAO().count(vo);
    }

    public List<T> list(BaseVo vo) {
        Integer rowCount = count(vo);
        vo.getPager().setRowCount(rowCount);
        return getDAO().list(vo);
    }

    public T getById(Object id) {
        return getDAO().getById(id);
    }
}
